﻿<!DOCTYPE HTML>
<html>

<head>
	<meta charset="utf-8">
	<meta name="renderer" content="webkit|ie-comp|ie-stand">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport"
		content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
	<meta http-equiv="Cache-Control" content="no-siteapp" />
	<link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css" />
	<link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css" />
	<link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css" />
	<link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin" />
	<link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css" />
	<title>报刊查询</title>
	<style>
		.summoney {
			float: right;
			text-align: left;
			border-radius: 10px;
		}

		.mystyle {
			padding-right: 20px;
			;
		}

		.mybtn {
			margin-left: 20px;
		}

		[v-cloak] {
			display: none;
		}
	</style>
</head>

<body>
	<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 报刊订阅 <span
			class="c-gray en">&gt;</span> 订阅报刊</nav>
	<div class="page-container">
		<div v-cloak class="cl pd-5 bg-1 bk-gray mt-20 mystyle"> <span class="summoney"> <i
					class="Hui-iconfont">&#xe670;</i>商品总额:
				{{summoney+'元'}}<button @click="ordernews" class="btn btn-primary mybtn">提交订单</button></span>
		</div>
		<div class="mt-20">
			<div class="mt-20">
				<table class="table table-border table-bordered table-bg table-hover table-sort table-responsive">
					<thead>
						<tr class="text-c">
							<th width="100">报刊号</th>
							<th>报刊名</th>
							<th width="120">报刊类型</th>
							<th>出版日期</th>
							<th width="120">报刊价格</th>
							<th width="100">订阅</th>
						</tr>
					</thead>
					<tbody>
						<tr v-cloak class="text-c" v-for="(item, index) in searchlist" :key="index">
							<td>{{ item.newsno }}</td>
							<td class="text-l">{{ item.newsname }}</td>
							<td>{{item.type}}</td>
							<td>{{ item.publish_date}}</td>
							<td>{{ item.money}}</td>
							<td :ref="'sub_'+index"><a title="订阅" href="javascript:;"
									@click="subnews(item.newsno,item.money,index)" class="ml-5"
									style="text-decoration:none"><i class="Hui-iconfont c-success">&#xe603;</i>
								</a>
							</td>
						</tr>
					</tbody>
				</table>
			</div>
		</div>


	</div>
	<!--_footer 作为公共模版分离出去-->
	<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
	<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
	<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
	<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script>
	<!--/_footer 作为公共模版分离出去-->

	<!--请在下方写此页面业务相关的脚本-->
	<script type="text/javascript" src="lib/My97DatePicker/4.8/WdatePicker.js"></script>
	<script type="text/javascript" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
	<script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>
	<script src="lib/vue.js"></script>
	<script type="text/javascript">
		$(function () {
			$('.table-sort').dataTable({
				"aaSorting": [
					[0, "asc"]
				], //默认第几个排序
				"bStateSave": true, //状态保存（保存排序状态）
				"pading": false,
				"aoColumnDefs": [{
						"orderable": false,
						"aTargets": [1, 2, 3]
					} // 不参与排序的列
				]
			});
		})
		var vm = new Vue({
			el: '.page-container',
			created() {
				var _this = this;
				this.username = sessionStorage.getItem("user")
				$.ajax({
					type: 'GET',
					url: `http://localhost:8080/newspaperlist?username='${this.username}' `,
					dataType: 'json',
					async: false,
					success: function (data) {
						_this.searchlist = data
					},
					error: function (err) {
						console.log(err.message);
					},
				});
			},
			data: {
				searchlist: [],
				newsprice: 0,
				orderlist: [],
				username: ''
			},
			methods: {
				subnews(orderid, price, index) {
					let current = this.$refs["sub_" + index][0].children[0]
					if (current.title == "订阅") {
						current.title = '取消'
						current.innerHTML = `<i class="Hui-iconfont c-red">&#xe66b;</i>`
						this.newsprice += parseFloat(price)
						if (this.orderlist.indexOf(orderid) === -1) {
							this.orderlist.push(orderid)
						}
						layer.msg('已加入购物车', {
							icon: 1,
							time: 1000
						})

					} else {
						layer.confirm('取消订单？', () => {
							current.title = '订阅'
							current.innerHTML = `<i class="Hui-iconfont c-success">&#xe603;</i>`
							this.newsprice -= parseFloat(price)
							let index = this.orderlist.indexOf(orderid)
							if (index !== -1) {
								this.orderlist.splice(index, 1)
								layer.msg('取消成功', {
									icon: 1,
									time: 1000
								})
							} else {
								layer.msg('取消失败，请稍后重试', {
									icon: 2,
									time: 1000
								})
							}

						})
					}
				},
				ordernews() {
					var _this = this;
					layer.confirm("确定提交？一旦提交不能再修改", () => {
						$.ajax({
							type: 'post',
							url: `http://localhost:8080/order`,
							data: {
								username: this.username,
								orderid: this.orderlist
							},
							success(data) {
								layer.msg(data, {
									icon: 1,
									time: 1000
								})
								setTimeout(() => location.replace(location.href), 1200)
							},
							error(err) {
								layer.msg("提交失败", {
									icon: 2,
									time: 1000
								})
							}
						})
					})
				}
			},
			computed: {
				summoney() {
					return this.newsprice
				}
			},
		})
	</script>
</body>

</html>